{include file="common/header"}
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form top-search-from" action="">
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" placeholder="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
            <div class="toggle-btn">
                <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
    </div>
</div>

<script type="text/html" id="toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-md" lay-event="recycle">
        <i class="layui-icon layui-icon-delete"></i>
        回收站
    </button>
</script>

<script type="text/html" id="options">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">
        <i class="layui-icon">编辑</i>
    </button>
    <button class="pear-btn pear-btn-warm pear-btn-xs" lay-event="more">
        <i class="layui-icon">更多</i>
    </button>
</script>

<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" {{#
           if(d.status==1){ }} checked {{# } }}>
</script>
{include file="common/footer"}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dropdown = layui.dropdown;

        let MODULE_PATH = "{$Request.root}/admin/";

        let cols = [[{
            type: 'checkbox'
        },
            {
                field: 'id',
                title: 'ID',
                sort: true,
                align: 'center',
                unresize: true
            }, {
                title: '账号',
                field: 'username',
                align: 'center',
                unresize: true,
            },
            {
                title: '昵称',
                field: 'nickname',
                align: 'center',
                unresize: true,
            },
            {
                title: '状态',
                field: 'enable',
                align: 'center',
                unresize: true,
                templet: '#status'
            },
            {
                field: 'create_time',
                title: '创建时间',
                unresize: true,
                align: 'center'
            },
            {
                field: 'update_time',
                title: '更新时间',
                unresize: true,
                align: 'center'
            },
            {
                title: '操作',
                toolbar: '#options',
                align: 'center',
                unresize: true
            }
        ]]

        table.render({
            elem: '#dataTable',
            url: MODULE_PATH + 'index',
            page: true,
            cols: cols,
            cellMinWidth: 100,
            skin: 'grid',
            even: 'true',
            size: 'lg',
            toolbar: '#toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'more') {
                dropdown.render({
                    elem: this, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    data: [{
                        title: '分配角色',
                        id: 'role'
                    }, {
                        title: '直接权限',
                        id: 'permission'
                    }, {
                        title: '删除账号',
                        id: 'remove'
                    }],
                    click: function (menudata) {
                        if (menudata.id === 'role') {
                            window.role(obj);
                        } else if (menudata.id === 'remove') {
                            window.remove(obj);
                        } else if (menudata.id === 'permission') {
                            window.permission(obj);
                        }
                    },
                    align: 'right', // 右对齐弹出
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                })
            }
        });

        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'recycle') {
                window.recycle(obj);
            }
        });

        form.on('submit(query)', function (data) {
            table.reload('dataTable', {
                where: data.field,
                page: {curr: 1}
            })
            return false;
        });

        form.on('switch(status)', function (data) {
            var status = data.elem.checked ? 1 : 2;
            var id = this.value;
            var load = layer.load();
            $.post('status', {status: status, id: id}, function (res) {
                layer.close(load);
                //判断有没有权限
                if (res && res.code == 999) {
                    layer.msg(res.msg, {
                        icon: 5,
                        time: 2000,
                    })
                    return false;
                } else if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1, time: 1500})
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1500}, function () {
                        $(data.elem).prop('checked', !$(data.elem).prop('checked'));
                        form.render()
                    })
                }
            })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增管理员',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改管理员',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'edit/id/' + obj.data['id']
            });
        }

        window.role = function (obj) {
            layer.open({
                type: 2,
                title: '分配角色',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'role/id/' + obj.data['id']
            });
        }

        window.permission = function (obj) {
            layer.open({
                type: 2,
                title: '分配直接权限',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'permission/id/' + obj.data['id']
            });
        }

        window.recycle = function () {
            layer.open({
                type: 2,
                title: '回收站',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'recycle',
                cancel: function () {
                    table.reload('dataTable');
                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该管理', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'remove',
                    data: {id: obj.data['id']},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            layer.confirm('确定要删除这些管理员', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('dataTable');
        }
    })
</script>
</body>
</html>
